package _09_高频题;

/**
 * https://leetcode-cn.com/problems/container-with-most-water/
 * @Author: haogege
 * @Date: 2021/9/3
 */
public class _11_盛最多水的容器 {

    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int maxArea = 0;
        while (left < right) {
            int minHeight = Math.min(height[left], height[right]);
            maxArea = Math.max(maxArea, (right - left) * minHeight);
            while (left < right && height[left] <= minHeight) left++;
            while (left < right && height[right] <= minHeight) right--;
        }
        return maxArea;
    }


    // 如果left++，right--,比当前还小，就没必要计算
    public int maxAre3(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int maxArea = 0;
        while (left < right) {
            if (height[left] < height[right]) {
                int minHeight = height[left];
                maxArea = Math.max(maxArea, (right - left) * minHeight);
                while (left < right && height[left] <= minHeight) left++;
            } else {
                int minHeight = height[right];
                maxArea = Math.max(maxArea, (right - left) * height[right]);
                while (left < right && height[right] <= minHeight) right--;
            }
        }
        return maxArea;
    }

    public int maxArea2(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int maxArea = 0;
        while (left < right) {
            int minHeight = height[left] < height[right] ? height[left++] : height[right--];
            maxArea = Math.max(maxArea, (right - left + 1) * minHeight);
        }
        return maxArea;
    }

    public int maxArea1(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int maxArea = 0;
        while (left < right) {
            if (height[left] < height[right]) {
                maxArea = Math.max(maxArea, (right - left) * height[left]);
                left++;
            } else {
                maxArea = Math.max(maxArea, (right - left) * height[right]);
                right--;
            }
        }
        return maxArea;
    }

}
